﻿#include "gametablemodel.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QDebug>
#include <QApplication>

GameTableModel::GameTableModel(QObject *parent)
    : QSqlQueryModel(parent)
{
    setQuery("SELECT * FROM data");
    setHeaderData(0, Qt::Horizontal, tr("id"));
    setHeaderData(1, Qt::Horizontal, tr("plantName"));
    setHeaderData(2, Qt::Horizontal, tr("qua"));
    setHeaderData(3, Qt::Horizontal, tr("score"));
    setHeaderData(4, Qt::Horizontal, tr("inverterName"));
    setHeaderData(5, Qt::Horizontal, tr("month"));
}

QVariant GameTableModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid() && role == CellRole) {
        if (index.column() == 1)
            return trPlantName(QSqlQueryModel::data(index, Qt::DisplayRole).toString());
        if (index.column() == 4)
            return trInverterName(QSqlQueryModel::data(index, Qt::DisplayRole).toString());
        return QSqlQueryModel::data(index, Qt::DisplayRole);
    }
  else {
      return QSqlQueryModel::data(index, role);
  }
}

QHash<int, QByteArray> GameTableModel::roleNames() const
{
    return {
        { CellRole, "TheTruth"}
    };
}

QString GameTableModel::trPlantName(const QString &plantName) const
{
    static const QHash<QString, const char *>greeting_strings = {
        {"KeZhou", QT_TR_NOOP("KeZhou")},
        {"AKeSu", QT_TR_NOOP("AKeSu")},
        {"HeJingYiXin", QT_TR_NOOP("HeJingYiXin")},
        {"WenSuRiYueHui", QT_TR_NOOP("WenSuRiYueHui")},
        {"MaiGaiTi", QT_TR_NOOP("MaiGaiTi")},
        {"HeJingTianHong", QT_TR_NOOP("HeJingTianHong")},
        {"TuMuShuKe", QT_TR_NOOP("TuMuShuKe")},
        {"HaMiHengXin", QT_TR_NOOP("HaMiHengXin")},
        {"HeShuoHengXin", QT_TR_NOOP("HeShuoHengXin")},
        {"HaMiShangde", QT_TR_NOOP("HaMiShangde")},
        {"HaMiJunXin", QT_TR_NOOP("HaMiJunXin")},
    };
    return tr(greeting_strings[plantName]);
}

QString GameTableModel::trInverterName(const QString &inverterName) const
{
    static const QHash<QString, const char *>greeting_strings = {
        {"YangGuang", QT_TR_NOOP("YangGuang")},
        {"HuiChuan", QT_TR_NOOP("HuiChuan")},
        {"KeHua", QT_TR_NOOP("KeHua")},
        {"ZhuiRi", QT_TR_NOOP("ZhuiRi")}
    };
    return tr(greeting_strings[inverterName]);
}

